package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.dashboard.bean.*;
import org.dashboard.service.*;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/permissions")
public class AdPermissionController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AdPermissionController.class);

    @Reference
    AdChartService adChartService;

    @Reference
    AdPageService adPageService;

    @Reference
    AdRoleService adRoleService;

    @Reference
    AdFolderService adFolderService;

    @Reference
    AdPermissionService adPermissionService;

    @Reference
    AdAuthorizationService adAuthorizationService;

    @RequestMapping(value = "/saveUsrRoles", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse saveUsrRoles(HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getParameter("userId"));
        String roleIds = request.getParameter("roleIds");

        List<Integer> roleIdList = Arrays.stream(roleIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());

        adPermissionService.saveUserRoles(roleIdList, userId);

        JsonResponse jsonResponse = new JsonResponse();

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/queryUsrRoles", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse queryUsrRoles(HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getParameter("userId"));

        JsonResponse jsonResponse = new JsonResponse();
        List<Map<String, Object>> roles = adRoleService.selectUserRoles(userId);

        jsonResponse.setData(roles);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/queryRoleResources", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse queryRoleResources(HttpServletRequest request) {
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        Integer resourceType = Integer.parseInt(request.getParameter("resourceType"));

        JsonResponse jsonResponse = new JsonResponse();
        List<AdAuthorization> authorizations = adAuthorizationService.queryRoleResources(roleId, resourceType);

        jsonResponse.setData(NameCastTools.listToListMap(authorizations));
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/saveRoleResources", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse saveRoleResources(HttpServletRequest request) {
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        Integer resourceType = Integer.parseInt(request.getParameter("resourceType"));
        String resourceIds = request.getParameter("resourceIds");

        JSONArray resourceIdJsonArray = (JSONArray) JSON.parse(resourceIds);
        ArrayList<Integer> resourceIdList = new ArrayList<>();
        if (resourceIdJsonArray != null) {
            int len = resourceIdJsonArray.size();
            for (int i = 0; i < len; i++) {
                resourceIdList.add(resourceIdJsonArray.getInteger(i));
            }
        }

        adPermissionService.saveRoleResources(roleId, resourceType, resourceIdList);

        return JsonResponse.success();
    }

}
